package com.irdstudio.apicenter.console.controller.appframe;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppTradeInout;
import com.irdstudio.apicenter.console.dm.AppTradePrivate;
import com.irdstudio.apicenter.console.service.appframe.AppFrameTradeService;
import com.irdstudio.apicenter.console.service.appframe.AppTradeInoutService;

/**
 * app应用管理
 * 
 * @author jr
 * 
 */
@Controller
@RequestMapping(value = "/appFrameTrade")
public class AppFrameTradeAction extends MacmsBaseAction {
	@Autowired
	private AppFrameTradeService appFrameTradeService;
	@Autowired
	private AppTradeInoutService appTradeInoutService;

	@RequestMapping(value = "jumpCreatePage")
	public String jumpCreatePage(HttpServletRequest request) {
		// String appId = super.context.getRequestParams().getValue("appid");
		// request.setAttribute("appid", appId);
		// request.setAttribute("context", super.context);
		String appId = request.getParameter("appid");
		request.setAttribute("appid", appId);
		return "appframe/createTrade";
	}

	@RequestMapping(value = "jumpEditPage")
	public String jumpEditPage(HttpServletRequest request) throws SQLException {
		// String appId = super.context.getRequestParams().getValue("appid");
		// request.setAttribute("appid", appId);
		// request.setAttribute("context", super.context);
		String tradeid = request.getParameter("tradeid");
		AppTradePrivate appTradePrivate = appFrameTradeService
				.queryAppTradePrivate(tradeid);
		List<AppTradeInout> listIn = appTradeInoutService.queryTradeInOrOut(
				tradeid, "1");
		List<AppTradeInout> listOut = appTradeInoutService.queryTradeInOrOut(
				tradeid, "2");
		appTradeInoutService.queryTradeInOrOut(tradeid, "2");
		String appId = request.getParameter("appid");
		request.setAttribute("appTradePrivate", appTradePrivate);
		request.setAttribute("listIn", listIn);
		request.setAttribute("listOut", listOut);
		request.setAttribute("appid", appId);
		return "appframe/editTrade";
	}

	@RequestMapping(value = "saveAppTradePrivate")
	public void saveAppFrameTrade(
			@ModelAttribute("appTradePrivate") AppTradePrivate appTradePrivate,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appFrameTradeService.insertOne(appTradePrivate);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}

	}

	@RequestMapping(value = "updateAppTradePrivate")
	public void updateAppTradePrivate(
			@ModelAttribute("appTradePrivate") AppTradePrivate appTradePrivate,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appFrameTradeService.update(appTradePrivate);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}

	}

	@RequestMapping(value = "delAppTradePrivate")
	public void deleteAppFrameTrade(HttpServletRequest request,
			HttpServletResponse response) throws ClassNotFoundException,
			SQLException {
		int flag = -1;
		String tradeId = request.getParameter("tradeid");
		flag = appFrameTradeService.delete(tradeId);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

}
